package com.lanhai.zhonglai.dispatch;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lanhai.zhonglai.MsgCore;
import com.lanhai.zhonglai.beans.AgvBean;
import com.lanhai.zhonglai.beans.AgvMountBean;
import com.lanhai.zhonglai.beans.DotBean;
import com.lanhai.zhonglai.beans.StationBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.AgvCurrentTransEnum;
import com.lanhai.zhonglai.enums.AgvMountTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IAgv;
import com.lanhai.zhonglai.interfaces.IAgvEvent;
import com.lanhai.zhonglai.interfaces.ICmdLog;
import com.lanhai.zhonglai.interfaces.IControlArea;
import com.lanhai.zhonglai.interfaces.IDot;
import com.lanhai.zhonglai.interfaces.IDotEvent;
import com.lanhai.zhonglai.interfaces.IRoad;
import com.lanhai.zhonglai.interfaces.ISendData;
import com.lanhai.zhonglai.interfaces.IStation;
import com.lanhai.zhonglai.interfaces.IStationMount;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.third.StationFactory;
//弃用
@Component
public class JudgementALD_S {

	@Autowired
	IStation iStation;

	@Autowired
	IStationMount iStationMount;

	@Autowired
	IAgv iAgv;

	@Autowired
	IDot iDot;

	@Autowired
	ISendData sendTools;

	@Autowired
	IAgvEvent iAgvEvent;

	@Autowired
	IDotEvent iDotEvent;

	@Autowired
	IRoad iRoad;

	@Autowired
	ICmdLog iCmdLog;

	@Autowired
	IControlArea iControlArea;

	@Autowired
	AgvCmdMap agvCmdMap;

	@Autowired
	StationLeaveCheck stationCheck;

	/**
	 * 只处理空车和同时传的
	 * 
	 * @param agv
	 * @param station
	 * @param dot
	 * @param agvMount
	 * @param dmount
	 */
	public void judgement_once(AgvBean agv, StationBean station, DotBean dot, AgvMountBean agvMount,
			StationMountBean dmount) {
		IStationProtocol stationEvent = StationFactory.getStation(station.getCmdSign());

		if (StationCmdMap.canLeave(station.getDotno())) {

			MsgCore.sendMsg(agv.getAgvId(), "机台允许离开");
			byte[] cmd = stationEvent.agvLeave(station.getLine(), station.getFillType());
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV离开", cmd,
					station.getStationname());
			if (iControlArea.isLockedForDot(dot.getDotno(), agv.getAgvId())) {
				// 前方交管区被管了
				return;
			}
			stationCheck.check(dot, agv, station, stationEvent, agvMount);
			return;

		}
		// ********************* 处理转动 **********************
		if (StationCmdMap.canReceive(station.getDotno()) && agv.getCurrentTrans() != AgvCurrentTransEnum.None) {
			// 传输过程中，直接过滤
			if (agv.isTransing()==false) {
				 agv.setTransing(true);
				 iAgv.saveCache(agv);
				switch (agv.getCurrentTrans()) {
				case DownIn:
					iAgvEvent.transDown(agv, true);
					break;
				case DownOut:
					iAgvEvent.transDown(agv, false);
					break;
				case UpIn:
					iAgvEvent.transUp(agv, true);
					break;
				case UpOut:
					iAgvEvent.transUp(agv, false);
					break;
				case UpInAndDownOut:
					iAgvEvent.transUpInAndDownOut(agv);
					break;
				case UpOutAndDownIn:
					iAgvEvent.transUpOutAndDownIn(agv);
					break;
				default:
					MsgCore.sendMsg(agv.getAgvId(), "未知的传输模式");
					break;
				}
			}
			return;
		}
		// **************** 处理传动 end ****************

		// **************** 处理传输状态 ********
		if(agvMount.getMountType()==AgvMountTypeEnum.AllFull&&agv.isUpHasTrans()==false) {
			agv.setUpHasTrans(true);
		}
		
		if (agvMount.getMountType() == AgvMountTypeEnum.AllEmpty && agv.isDownHasTrans()
				&& agv.isFinishTrans() == false) {
			// 发料后的全空
			agv.setFinishTrans(true);
			iAgv.saveCache(agv);
		} else if (agvMount.getMountType() == AgvMountTypeEnum.UpFullAndDownEmpty && agv.isFinishTrans() == false) {
			// 同时送料后的全空
			agv.setFinishTrans(true);
			iAgv.saveCache(agv);
		}
		

		if (agv.isFinishTrans()) {
			// 申请离开
			if (agv.getLastSaveDot() != dot.getDotno()) {
				iCmdLog.saveTransLog(agv.getAgvId(), dot.getDotno(), station, agv.getApplyTransType());

				agv.setLastSaveDot(dot.getDotno());
				iAgv.saveCache(agv);
			}

			MsgCore.sendMsg(agv.getAgvId(), "AGV申请离开");
			byte[] goout = stationEvent.applyLeave(station.getLine(), station.getFillType());
			sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请离开", goout,
					station.getStationname());
			return;
		}

		// **************** 处理传输状态 end ********

		// 机台允许传输，开始申请皮带转动（申请传输）
		if (StationCmdMap.canTrans(station.getDotno())) {

			if (dmount.isAllowIn() && dmount.isAllowOut()
					&& agvMount.getMountType() == AgvMountTypeEnum.UpEmptyAndDownFull) {
				// 机台上下都OK，AGV没有发生过传输，AGV上空下满，申请同时传输，这是最好的情况
				MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-双层");
				if (agvMount.isDownFull()) {
					// 下满，上入下出
					agv.setCurrentTrans(AgvCurrentTransEnum.UpInAndDownOut);
				} else {
					// 上满，上出下入
					agv.setCurrentTrans(AgvCurrentTransEnum.UpOutAndDownIn);
				}
				agv.setApplyTransType(TransDirectionEnum.Both);
				agv.setBothTrans(true);
				iAgv.saveCache(agv);

				byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.Both);
				sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请双层", cmd,
						station.getStationname());
				return;
			} else {

				// 空车收，下面有车的发
				if (agvMount.getMountType() == AgvMountTypeEnum.AllEmpty) {
					// 只收货
					if (dmount.isAllowOut()) {
						// AGV上层收货
						agv.setCurrentTrans(AgvCurrentTransEnum.UpIn);
						agv.setBothTrans(false);
						agv.setApplyTransType(TransDirectionEnum.OnlyOut);
						iAgv.saveCache(agv);
						MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-传入");
						byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyOut);
						sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传入", cmd,
								station.getStationname());
					} else {
						// 机台数量不足，不发起申请
						MsgCore.sendMsg(agv.getAgvId(),
								String.format("待传输花篮数量，上料%d,出料%d", dmount.getInNum(), dmount.getOutNum()));

					}
					return;
				} else if (agvMount.getMountType() == AgvMountTypeEnum.UpEmptyAndDownFull
						||agvMount.getMountType()==AgvMountTypeEnum.AllFull) {
					// 只发货
					if (dmount.isAllowIn()) {
						agv.setCurrentTrans(AgvCurrentTransEnum.DownOut);
						agv.setBothTrans(false);
						agv.setApplyTransType(TransDirectionEnum.OnlyIn);
						iAgv.saveCache(agv);
						MsgCore.sendMsg(agv.getAgvId(), "AGV发起申请传输-传出");
						byte[] cmd = stationEvent.applyRecieve(station.getLine(), TransDirectionEnum.OnlyIn);
						sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV申请传出", cmd,
								station.getStationname());

					} else {
						MsgCore.sendMsg(agv.getAgvId(),
								String.format("待传输花篮数量，上料%d,出料%d", dmount.getInNum(), dmount.getOutNum()));

					}
					return;
				} else {
					// 其它状态直接离开
					byte[] goout = stationEvent.agvLeave(station.getLine(), station.getFillType());
					sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV已离开", goout,
							station.getStationname());
					return;
				}
			}
		}

		// ******** 机台允许传输 end ********

		if(agvMount.getMountType()==AgvMountTypeEnum.AllEmpty) {
			if(dmount.isAllowOut() ) {
				byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.Both);
				sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
						station.getStationname());
			} else {
				//量不够，不用AGV就位
				MsgCore.sendMsg(agv.getAgvId(), String.format("待传输花篮数量，上料%d,出料%d", dmount.getInNum(),dmount.getOutNum()) );
			 } 
		}else {
			//AGV下出上入的状态（上空下满）
			  
			if(dmount.isAllowIn() ) { 
				byte[] cmd = stationEvent.agvArrived(station.getLine(), TransDirectionEnum.Both);
				sendTools.sendStationData(station.getIp(), station.getPort(), station.getLine(), "AGV就位", cmd,
						station.getStationname());
			} else {
				//量不够，不用AGV就位
				MsgCore.sendMsg(agv.getAgvId(), String.format("待传输花篮数量，上料%d,出料%d", dmount.getInNum(),dmount.getOutNum()) );
			 } 
		}
		
	}
}
